﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace LegendHub.Crypt
{
    public class DES
    {
        private static string _key;

        private static string _vector;

        public static string IV
        {
            get
            {
                return _vector;
            }
            set
            {
                _vector = value;
            }
        }

        public static string Key
        {
            get
            {
                return _key;
            }
            set
            {
                _key = value;
            }
        }

        static DES()
        {
            _key = "k-8DO+{}";
            _vector = "%2n67}A\\fZ@k*:'Y";
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="plainStr">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string DESEncrypt(string plainStr)
        {
            var bytes = Encoding.UTF8.GetBytes(_key);
            var numArray = Encoding.UTF8.GetBytes(_vector);
            var bytes1 = Encoding.UTF8.GetBytes(plainStr);
            string base64String = null;
            var dEsCryptoServiceProvider = new DESCryptoServiceProvider();
            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(memoryStream, dEsCryptoServiceProvider.CreateEncryptor(bytes, numArray), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(bytes1, 0, bytes1.Length);
                        cryptoStream.FlushFinalBlock();
                        base64String = Convert.ToBase64String(memoryStream.ToArray());
                    }
                }
            }
            catch
            {
                // ignored
            }
            dEsCryptoServiceProvider.Clear();
            return base64String;
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="encryptStr">加密过的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string DESDecrypt(string encryptStr)
        {
            var bytes = Encoding.UTF8.GetBytes(_key);
            var numArray = Encoding.UTF8.GetBytes(_vector);
            var numArray1 = Convert.FromBase64String(encryptStr);
            string str = null;
            var dEsCryptoServiceProvider = new DESCryptoServiceProvider();
            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(memoryStream, dEsCryptoServiceProvider.CreateDecryptor(bytes, numArray), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(numArray1, 0, numArray1.Length);
                        cryptoStream.FlushFinalBlock();
                        str = Encoding.UTF8.GetString(memoryStream.ToArray());
                    }
                }
            }
            catch
            {
                // ignored
            }
            dEsCryptoServiceProvider.Clear();
            return str;
        }
    }
}
